#' 分析多变量
#'
#' @param MV_Input M4_mv_dat_to_MV_Input方法生成的MV_Input
#' @param mv_method c( "mr_mvivw","mr_mvegger","mr_mvlasso","mr_mvmedian")，
#' 至少要有"mr_mvivw"方法
#'
#' @return 所有方法的结果
#' @export
#'
#' @examples
#'
#' \dontrun{
#'
#' library(Oneclick)
#'
#' MV_Input<- M4_mv_dat_to_MV_Input( mv_dat )
#'
#' mv_res_all <- M5_mvmr(MV_Input)
#'
#'
#' # 计算条件F值
#'
#' strength_mvmr <- M7_strength_mvmr(MV_Input)
#'
#' }
#'
#'
M5_mvmr<-function(MV_Input = MV_Input,
                  mv_method = c( "mr_mvivw",
                                 "mr_mvegger",
                                 "mr_mvlasso",
                                 "mr_mvmedian")   ){
  stopifnot("mr_mvivw" %in% mv_method )
  mv_res_all <- MendelianRandomization::mr_mvivw(MV_Input)
  mv_res_all <- data.frame( exposure = mv_res_all@Exposure,
                            outcome = mv_res_all@Outcome,
                            nsnp = mv_res_all@SNPs,
                            method = "mr_mvivw",
                            b = mv_res_all@Estimate,
                            se = mv_res_all@StdError,
                            pval = mv_res_all@Pvalue) %>%
    TwoSampleMR::generate_odds_ratios()

  if("mr_mvegger" %in% mv_method ){
    mv_res_temp <- MendelianRandomization::mr_mvegger(MV_Input)
    mv_res_temp <- data.frame( exposure = mv_res_temp@Exposure,
                               outcome = mv_res_temp@Outcome,
                               nsnp = mv_res_temp@SNPs,
                               method = "mr_mvegger",
                               b = mv_res_temp@Estimate,
                               se = mv_res_temp@StdError.Est,
                               pval = mv_res_temp@Pvalue.Est) %>%
      TwoSampleMR::generate_odds_ratios()
    mv_res_all <- dplyr::bind_rows(mv_res_all,mv_res_temp )
  }

  if("mr_mvmedian" %in% mv_method ){
    mv_res_temp <- MendelianRandomization::mr_mvmedian(MV_Input)
    mv_res_temp <- data.frame( exposure = mv_res_temp@Exposure,
                               outcome = mv_res_temp@Outcome,
                               nsnp = mv_res_temp@SNPs,
                               method = "mr_mvmedian",
                               b = mv_res_temp@Estimate,
                               se = mv_res_temp@StdError,
                               pval = mv_res_temp@Pvalue) %>%
      TwoSampleMR::generate_odds_ratios()
    mv_res_all <- dplyr::bind_rows(mv_res_all,mv_res_temp )
  }

  if("mr_mvlasso" %in% mv_method ){
    mv_res_temp <- MendelianRandomization::mr_mvlasso(MV_Input)
    mv_res_temp <- data.frame( exposure = mv_res_temp@Exposure,
                               outcome = mv_res_temp@Outcome,
                               nsnp = mv_res_temp@SNPs,
                               method = "mr_mvlasso",
                               b = mv_res_temp@Estimate,
                               se = mv_res_temp@StdError,
                               pval = mv_res_temp@Pvalue) %>%
      TwoSampleMR::generate_odds_ratios()
    mv_res_all <- dplyr::bind_rows(mv_res_all,mv_res_temp )
  }

  mv_res_all$`Beta (95% CI)` <- ifelse(is.na(mv_res_all$b), NA,
                                   sprintf("%.3f (%.3f to %.3f)",
                                           mv_res_all$b, mv_res_all$lo_ci, mv_res_all$up_ci))


  mv_res_all$`OR (95% CI)` <- ifelse(is.na(mv_res_all$or), NA,
                                 sprintf("%.3f (%.3f to %.3f)",
                                         mv_res_all$or, mv_res_all$or_lci95, mv_res_all$or_uci95))

  return(mv_res_all)
}
